"""
@author: chen
@file: 面向对象-三乐购物系统.py
@time: 2023/4/13 15:55
"""


# 购物系统类
# 实例属性 -- 商品信息、系统说明信息、当前多个用户信息
# 方法 -- 登录注册、查看商品、添加购物车、结算

# 用户类
# 实例属性 -- 名字、密码、钱、购物车
# 方法
# user class
class User:
    def __init__(self, name, passwd, init_amount):
        self.name = name
        self.passwd = passwd
        self.init_amount = init_amount
        self.shopping_car = {}

class ShoppingSystem:
    def __init__(self, goods_info, system_info):
        self.goods_info = goods_info
        self.system_info = system_info
        self.userdict = {}

    def register(self, name, passwd, initial_amount):
        if name not in self.userdict:
            # 创建一个新用户，实例化User类
            user = User(name, passwd, initial_amount)
            # 名字作为key，user对象作为value，存在self.userdict里面
            self.userdict[name] = user
        else:
            print("用户已存在")

    def login(self, name, passwd):
        if name in self.userdict:
            if passwd == self.userdict[name].passwd:
                print("登录成功！")
                return False
            else:
                print("密码错误,请重新输入")
        else:
            print("没有这个用户，请重新输入")
        return True

    def show_goods(self):
        print("商品信息：")
        for i, j in self.goods_info.items():
            print(f"商品编号：{i}, 商品：{j['name']}, 价格：{j['price']}")

    def show_car(self, name):
        car = self.userdict[name].shopping_car
        print("购物车中的商品信息：")
        for i in car:
            print(f"编号：{i}，名称：:{self.goods_info[i]['name']}，价格：{self.goods_info[i]['price']}")

    def goods_add(self, purchase, name):
        if purchase not in self.goods_info:
            print("商品不存在")
        else:
            self.userdict[name].shopping_car[purchase] = self.goods_info[purchase]['name']
            print("加购成功！")


fruit_goods = {
    'F001': {"name": "apple", "price": 8},
    'F002': {"name": "banana", "price": 4}
}
medicine_goods = {
    'M001': {"name": "奥司他韦", "price": 60},
    'M002': {"name": "抗病毒口服液", "price": 30}
}

system1 = ShoppingSystem(fruit_goods, "水果购物系统")
system2 = ShoppingSystem(medicine_goods, "药品购物系统")

system = [system1, system2]

while 1:
    print("当前平台有购物系统如下：")
    for k, v in enumerate(system):
        print(f"{k}.{v.system_info} ")
    c1 = input("请输入你的选择（按q退出）：")
    if c1 == "q":
        break
    if c1.isdigit() and int(c1) < len(system):
        c1 = int(c1)
        current_system = system[c1]
        print(f"欢迎进入{current_system.system_info}！".center(40, "*"))
        #当前登录用户
        current_user = None
        while 1:
            c2 = input("1.登录\n2.注册\n3.查看商品\n4.查看购物车\n5.添加商品\n6.结算购物车\n请输入你的选择（按q返回）：")
            if c2 == "q":
                break
            if c2 == "1":
                flag = 1
                while flag:
                    current_user, passwd = input("请输入名字、密码（用空白分隔）").split()
                    flag = current_system.login(current_user, passwd)
            elif c2 == "2":
                name, passwd, amount = input("请输入名字、密码、初始金额（用空白分隔）").split()
                current_system.register(name, passwd, amount)
                print(f"当前系统用户有：{current_system.userdict}")
            elif c2 == "3":
                current_system.show_goods()
            elif c2 == "4":
                if current_user == None:
                    print("请先登录!")
                else:
                    current_system.show_car(current_user)
            elif c2 == "5":
                if current_user == None:
                    print("请先登录")
                else:
                    while 1 :
                        purchased = input("请输入你想添加的商品编号（按q结束添加）：")
                        if purchased == "q":
                            break
                        else:
                            current_system.goods_add(purchased, current_user)
            elif c2 == "6":
                if current_user == None:
                    print("请先登录")
                else:
                    pass

    else:
        print("输入有误，请重新输入（按q退出）：")